<?php 
include_once '../lib/check.php';


//--------------------数据库层函数----------------------//
class db
{
    function dbInit()
    {
        $db   = parse_ini_file(SYSTEM_CONFIG.'config.ini');
        $link = mysql_connect($db['host'], $db['user'], $db['pwd']);
        if ($link) {
            mysql_select_db($db['dbname']);
            return $link;
        } 
    }
    
    //------------------添加类方法---------------------//
    function insert($columns, $values, $type, $table)
    {
        $sql = 'insert into `'.$table.'` (';
        foreach ($columns as $column) {
            $sql .= $column.', ';
        }
        $sql = substr($sql, '0', '-2').') ';
        if ($type == 'single') {
            $sql .= 'value(';
            foreach ($values as $value) {
                $sql .= "'".$value."', ";
            }
            $sql = substr($sql, '0', '-2').');';
        }
        
        
        if ($type == 'multi') {
            $sql .= 'values';
            foreach ($values as $value) {
                $sql .= '(';
                foreach ($value as $v) {
                    $sql .= "'".$v."', ";
                }
                $sql = substr($sql, '0', '-2').'),';
            }
            $sql = substr($sql, '0', '-1').';';
        }
        
        if (db::query($sql)) {
            return true;
        }
        return false;
    }
    

    //------------------删除类方法---------------------//
    function deleteById($id, $table)
    {
        $sql = "delete from `".$table."` where id=".$id;
        if (db::query($sql)) {
            return true;
        }
        return false;
    }
    
    function deleteByIds($ids, $table)
    {
        $sql = "delete from `".$table."` where ";
        foreach ($ids as $id) {
            $sql .= "id = '".$id."' or ";
        }
        $sql = substr($sql, '0', '-4');
        echo $sql;
        if (db::query($sql)) {
            return true;
        }
        return false;
    }
    
    function deleteByFilter($filter, $value, $table)
    {
        $sql = "delete from `".$table."` where `".$filter."`='".$value."'";
        if (db::query($sql)) {
            return true;
        }
        return false;
    }
    
    function deleteByFilters($filters, $type, $table)
    {
        $sql = "delete from `".$table."` where ";
        if ($type == 'and' || $type == 'or') {
            return false;
        }
        foreach ($filters as $k => $v) {
            $sql .= "`".$k."` = '".$v."' ".$type." ";
        }
        $sql = substr($sql, '0', '-4');
        if (db::query($sql)) {
            return true;
        }
        return false;
    }
    
    
    //------------------修改类方法---------------------//
    function updateById($opts, $id, $table)
    {
        return updateByFilter($opts, 'id', $id, $table);
    }
    
    function updateByFilter($opts, $filter, $value, $table)
    {
        $sql = "update `".$table."` set ";
        foreach ($opts as $k => $v) {
            $sql .= "`".$k."` = '".$v."', ";
        }
        $sql = substr($sql, '0', '-2')." where `".$filter."` = '".$value."'";
        if (db::query($sql)) {
            return true;
        }
        return false;
    }
    
    function updateByFilters($opts, $filters, $type, $table)
    {
        $sql = "update `".$table."` set ";
        foreach ($opts as $k => $v) {
            $sql .= "`".$k."` = '".$v."', ";
        }
        $sql = substr($sql, '0', '-2')." where ";
        if ($type != 'and' && $type != 'or') {
            return false;
        }
        foreach ($filters as $k => $v) {
            $sql .= "`".$k."` = '".$v."' ".$type." ";
        }
        $sql = substr($sql, '0', '-4');
        echo $sql;
        if (db::query($sql)) {
            return true;
        }
        return false;
    }
    
    
    
    //------------------查询类方法---------------------//
    function selectById($id, $table)
    {
        $sql = 'select * from `'.$table.'` where id = '.$id;
        return db::fetchData(db::query($sql), 'single');
        
    }
    
    function selectByFilter($column, $value, $table)
    {
        $sql = 'select * from `'.$table.'` where `'.$column."` = '".$value."'";
        return db::fetchData(db::query($sql), 'single');
    }
    
    function selectByFilters($filters, $type, $table, $order = null, $sort = 'DESC')
    {
        $sql = 'select * from `'.$table.'` where ';
        if ($type != 'and' && $type != 'or') {
            return false;
        }
        foreach ($filters as $k => $v) {
            $sql .= $k." = '".$v."' ".$type." ";
        }
        $sql = substr($sql, '0', '-4')." order by `".$order."` ".$sort;
        echo $sql.'<br>';
        return db::fetchData(db::query($sql), 'multi');
    }
    
    function selectOptsByFilters($opts, $filters, $type, $table, $order = null, $sort = 'DESC')
    {
        $sql = 'select ';
        foreach ($opts as $opt) {
            $sql .= "'".$opt."', ";
        }
        $sql = substr($sql, '0', '-2').' from `'.$table.'` where ';
        if ($type != 'and' && $type != 'or') {
            return false;
        }
        foreach ($filters as $k => $v) {
            $sql .= $k." = '".$v."' ".$type." ";
        }
        $sql = substr($sql, '0', '-4')." order by `".$order."` ".$sort;
        return db::fetchData(db::query($sql), 'multi');
    }
    
    function selectBySql($sql, $type)
    {
        return db::fetchData(db::query($sql), $type);
    }
    
    //-------------------------数据库执行-------------------------//
    function query($sql)
    {
        $link = db::dbInit();
        $result = mysql_query($sql, $link);
        mysql_close($link);
        return $result;
    }
        
    function fetchData($result, $type)
    {
        if ($type == 'single') {
            $results = mysql_fetch_array($result, MYSQL_ASSOC);
        }
        if ($type == 'multi') {
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $results[]=$row;
            }
            
        }
        return $results;
    }
}

//$res = db::selectByFilter('id', 1, 'role');
//var_dump($res);
